package com.ruoyi.crm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.crm.domain.CrmCustomer;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crm.domain.CrmCost;
import com.ruoyi.crm.service.ICrmCostService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 费用管理Controller
 *
 * @author ruoyi
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/crm/cost")
public class CrmCostController extends BaseController {
    @Autowired
    private ICrmCostService crmCostService;

    /**
     * 查询费用管理列表
     */
    @PreAuthorize("@ss.hasPermi('crm:cost:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmCost crmCost) {
        startPage();
        List<CrmCost> list = crmCostService.selectCrmCostList(crmCost);
        return getDataTable(list);
    }

    /**
     * 导出费用管理列表
     */
    @PreAuthorize("@ss.hasPermi('crm:cost:export')")
    @Log(title = "费用管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmCost crmCost) {
        List<CrmCost> list = crmCostService.selectCrmCostList(crmCost);
        ExcelUtil<CrmCost> util = new ExcelUtil<CrmCost>(CrmCost.class);
        util.exportExcel(response, list, "费用管理数据");
    }
    /**
     * 获取费用管理
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(crmCostService.getById(id));
    }

    /**
     * 修改费用管理
     */
    @PutMapping
    public AjaxResult edit(@RequestBody CrmCost crmCost) {
        if(crmCost.getApplyAscription().equals("0")){
            crmCost.setApplyGroupId(" ");
            crmCost.setApplyDeptId(" ");
        }else if(crmCost.getApplyAscription().equals("1")){
            crmCost.setApplyUserId(" ");
            crmCost.setApplyGroupId(" ");
        }else if(crmCost.getApplyAscription().equals("2")){
            crmCost.setApplyUserId(" ");
            crmCost.setApplyDeptId(" ");
        }

        crmCost.setIsDispose("1");
        return toAjax(crmCostService.updateById(crmCost));
    }

}
